package cn.iocoder.yudao.module.forward.controller.admin.broker;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.broker.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.broker.BrokerDO;
import cn.iocoder.yudao.module.forward.service.broker.BrokerService;

@Tag(name = "管理后台 - 转发服务")
@RestController
@RequestMapping("/forward/broker")
@Validated
public class BrokerController {

    @Resource
    private BrokerService brokerService;

    @PostMapping("/create")
    @Operation(summary = "创建转发服务")
    @PreAuthorize("@ss.hasPermission('forward:broker:create')")
    public CommonResult<Integer> createBroker(@Valid @RequestBody BrokerSaveReqVO createReqVO) {
        return success(brokerService.createBroker(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新转发服务")
    @PreAuthorize("@ss.hasPermission('forward:broker:update')")
    public CommonResult<Boolean> updateBroker(@Valid @RequestBody BrokerSaveReqVO updateReqVO) {
        brokerService.updateBroker(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除转发服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:broker:delete')")
    public CommonResult<Boolean> deleteBroker(@RequestParam("id") Integer id) {
        brokerService.deleteBroker(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得转发服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:broker:query')")
    public CommonResult<BrokerRespVO> getBroker(@RequestParam("id") Integer id) {
        BrokerDO broker = brokerService.getBroker(id);
        return success(BeanUtils.toBean(broker, BrokerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得转发服务分页")
    @PreAuthorize("@ss.hasPermission('forward:broker:query')")
    public CommonResult<PageResult<BrokerRespVO>> getBrokerPage(@Valid BrokerPageReqVO pageReqVO) {
        PageResult<BrokerDO> pageResult = brokerService.getBrokerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BrokerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出转发服务 Excel")
    @PreAuthorize("@ss.hasPermission('forward:broker:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBrokerExcel(@Valid BrokerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BrokerDO> list = brokerService.getBrokerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "转发服务.xls", "数据", BrokerRespVO.class,
                        BeanUtils.toBean(list, BrokerRespVO.class));
    }

}